using Microsoft.Phone.Controls.Maps.Core;
using System;
using System.Device.Location;
using System.Windows;
namespace Microsoft.Phone.Controls.Maps
{
	public class RoadMode : MercatorMode
	{
		private const string lowDPIMarketsKey = "LowDPIMarkets";
		private const string subdomainsKey = "RoadUriSubdomains";
		private const string uriFormatKey = "RoadUriFormat";
		private const string uriLowDPIFormatKey = "RoadLowDPIUriFormat";
		private const string coverageMapKey = "RoadCoverageMap";
		private readonly MapTileLayer tileLayer;
		private readonly TfeTileSource tileSource;
		private bool addedToMap;
		private Uri coverageMapUri;
		private bool restrictMaxZoomLevel;
		private string[] lowDPIMarkets = new string[0];
		private string tileUriFormat;
		private string tileLowDPIUriFormat;
		private bool configurationLoaded;
		private CopyrightManager copyright;
		public bool ShouldRestrictMaxZoomLevel
		{
			get
			{
				return this.restrictMaxZoomLevel;
			}
			set
			{
				this.restrictMaxZoomLevel = value;
				this.SetTfeCoverageMap();
			}
		}
		public int UpSampleLevelDelta
		{
			get
			{
				return this.tileLayer.UpSampleLevelDelta;
			}
			set
			{
				this.tileLayer.UpSampleLevelDelta = value;
			}
		}
		public override UIElement Content
		{
			get
			{
				return this.tileLayer;
			}
		}
		public override ModeBackground ModeBackground
		{
			get
			{
				return ModeBackground.Light;
			}
		}
		public override bool IsDownloading
		{
			get
			{
				return this.tileLayer != null && this.tileLayer.IsDownloading;
			}
		}
		public override bool IsIdle
		{
			get
			{
				return this.configurationLoaded && this.tileLayer != null && this.tileLayer.IsIdle;
			}
		}
		public RoadMode()
		{
			this.restrictMaxZoomLevel = false;
			this.tileLayer = new MapTileLayer(this);
			this.tileLayer.UpSampleLevelDelta = 7;
			this.tileSource = new TfeTileSource();
			this.tileLayer.TileSources.Add(this.tileSource);
			this.copyright = CopyrightManager.GetInstance();
			MapConfiguration.GetSection("v1", "WP7SLMapControl", null, new MapConfigurationCallback(this.AsynchronousConfigurationLoaded));
		}
		protected override Range<double> GetZoomRange(GeoCoordinate center)
		{
			return base.GetZoomRange(this.tileSource, center);
		}
		private void AsynchronousConfigurationLoaded(MapConfigurationSection config, object userState)
		{
			if (config != null)
			{
				string text = config["LowDPIMarkets"];
				if (!string.IsNullOrEmpty(text))
				{
					this.lowDPIMarkets = text.Split(new char[]
					{
						';'
					});
				}
				this.tileUriFormat = config["RoadUriFormat"];
				this.tileLowDPIUriFormat = config["RoadLowDPIUriFormat"];
				string[][] subdomains;
				if (config.Contains("RoadUriSubdomains") && MercatorMode.TryParseSubdomains(config["RoadUriSubdomains"], out subdomains))
				{
					this.tileSource.SetSubdomains(subdomains);
				}
				this.UpdateTileSource();
				if (config.Contains("RoadCoverageMap"))
				{
					this.coverageMapUri = new Uri(config["RoadCoverageMap"].Replace("{UriScheme}", "HTTP"), 1);
					if (this.addedToMap)
					{
						this.SetupMode();
					}
				}
			}
			this.configurationLoaded = true;
		}
		protected override void OnCultureChanged()
		{
			if (this.addedToMap)
			{
				this.SetupMode();
			}
		}
		internal override void UpdateAttribution()
		{
			this.copyright.RequestCopyrightString(MapStyle.Road, this.Center, this.ZoomLevel, this.CredentialsProvider, base.Culture, new Action<CopyrightResult>(this.CopyrightCallback));
		}
		private void CopyrightCallback(CopyrightResult result)
		{
			if (result.Culture == base.Culture && result.Location == this.Center && result.ZoomLevel == this.ZoomLevel)
			{
				base.UpdateAttribution(result.CopyrightStrings);
			}
		}
		public override void Activating(MapMode previousMode, MapLayerBase modeLayer, MapLayerBase modeForegroundLayer)
		{
			base.Activating(previousMode, modeLayer, modeForegroundLayer);
			this.SetupMode();
			this.addedToMap = true;
		}
		private void SetupMode()
		{
			this.UpdateTileSource();
			base.UpdateAttribution(ProjectionUpdateLevel.None);
			this.SetTfeCoverageMap();
		}
		private void UpdateTileSource()
		{
			if (base.Culture != null)
			{
				string text = base.Culture.get_Name().ToLowerInvariant();
				bool flag = false;
				string[] array = this.lowDPIMarkets;
				int i = 0;
				while (i < array.Length)
				{
					string text2 = array[i];
					if (string.IsNullOrEmpty(text2) || !(text2.ToLowerInvariant() == text))
					{
						i++;
					}
					else
					{
						flag = true;
						IL_56:
						if (flag && !string.IsNullOrEmpty(this.tileLowDPIUriFormat))
						{
							this.tileSource.UriFormat = this.tileLowDPIUriFormat.Replace("{Culture}", base.Culture.get_Name());
							return;
						}
						if (!string.IsNullOrEmpty(this.tileUriFormat))
						{
							this.tileSource.UriFormat = this.tileUriFormat.Replace("{Culture}", base.Culture.get_Name());
							return;
						}
						return;
					}
				}
				goto IL_56;
			}
		}
		private void SetTfeCoverageMap()
		{
			this.tileSource.SetCoverageMapUri(this.restrictMaxZoomLevel ? this.coverageMapUri : null);
		}
	}
}
